﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLib;

namespace MLO
{
    public class Frame : IFrame
    {
        public Frame()
        {
        }

        public Frame(string name, FrameType type)
        {
            Name = name;
            Type = type;
        }

        private List<ISlot> Slots = new List<ISlot>();
        private List<ISlot> SubFrameSlots = new List<ISlot>();
        private ISlot NextSlot = null;
        private ISlot ErrorSlot = null;
        private List<ISlot> ProductionSlots = new List<ISlot>();
        private List<ISlot> ResultSlots = new List<ISlot>();
        private List<ISlot> RegularSlots = new List<ISlot>();
        private ISlot AKOSlot = null;
        private List<ISlot> QuestionSlots = new List<ISlot>();

        public bool AddSlot(Slot slot)
        {
            if (Slots.Find(o => o.Name == slot.Name) == null)
            {
                switch (slot.Type)
                {
                    case SlotType.SubFrame:
                        SubFrameSlots.Add(slot);
                        break;
                    case SlotType.Next:
                        if (NextSlot != null)
                            NextSlot = slot;
                        else
                            return false;
                        break;
                    case SlotType.Error:
                        if (ErrorSlot != null)
                            ErrorSlot = slot;
                        else
                            return false;
                        break;
                    case SlotType.Production:
                        ProductionSlots.Add(slot);
                        break;
                    case SlotType.Result:
                        ResultSlots.Add(slot);
                        break;
                    case SlotType.Regular:
                        RegularSlots.Add(slot);
                        break;
                    case SlotType.AKO:
                        if (AKOSlot != null)
                            AKOSlot = slot;
                        else
                            return false;
                        break;
                    case SlotType.Question:
                        QuestionSlots.Add(slot);
                        break;
                }
                Slots.Add(slot);
                return true;
            }
            else
                return false;
        }

        public string ID { get; set; }
        public string Name { get; set; }
        public List<ISlot> GetSlots()
        {
            return Slots;
        }
        public List<ISlot> GetSlots(SlotType st)
        {
            switch (st)
            {
                case SlotType.SubFrame:
                    return SubFrameSlots;
                case SlotType.Next:
                    {
                        List<ISlot> result = new List<ISlot>();
                        if (NextSlot != null)
                            result.Add(NextSlot);
                        return result;
                    }
                case SlotType.Error:
                    {
                        List<ISlot> result = new List<ISlot>();
                        if (ErrorSlot != null)
                            result.Add(ErrorSlot);
                        return result;
                    }
                case SlotType.Production:
                    return ProductionSlots;
                case SlotType.Result:
                    return ResultSlots;
                case SlotType.Regular:
                    return RegularSlots;
                case SlotType.AKO:
                    {
                        List<ISlot> result = new List<ISlot>();
                        if (AKOSlot != null)
                            result.Add(AKOSlot);
                        return result;
                    }
                case SlotType.Question:
                    return QuestionSlots;
                default: return null;
            }
        }

        public ISlot SearchSlot(string id)
        {
            return Slots.Find(o => o.Name == id);
        }
        public FrameType Type { get; set; }
    }
}
